* ======================================
* --------- Import occupations ---------
* ======================================

	
* 2015
* ====
	* Only main employer listed
	use "$dta_raw\dta5\REGSYS_2015.dta", clear

	rename w19 idPers
	rename arb_yrke_styrk08 occupation2015
	rename frtk_sektor_2014 sector2015
	keep idPers occupation2015 sector2015
	drop if occupation2015 == ""

	gen year = 2015
	sort idPers
	save "$dta\ImportOccup15.dta",replace

	
	
* 2019
* ====

	* Multiple occups. per pers. Find main employer by income
	use $dta_raw\dta9\W19_0078_AMELD_STATDATA_2019_M11, clear
	drop if lopenr_virksomhet == ""
	egen income = rowtotal(lonn_*)
	bysort w19 (arb_stillingspst): gen n = _n
	gsort w19 - income
	bysort w19: gen main = 1*(_n == 1)
	keep if main == 1
	sum income
	gen year = 2019
	keep w19 lopenr_virksomhet main year
	rename lopenr_virksomhet idFirm
	rename w19 idPers

	tempfile main
	save `main'
	
	use "$dta_raw\dta9\W19_0078_REGSYS_2019.dta", clear

	rename w19_0078_lopenr_person idPers
	rename w19_0078_lopenr_virksomhet idFirm	
	rename arb_yrke_styrk08 occupation2019
	rename frtk_sektor_2014 sector2019
	keep idPers idFirm occupation2019 sector2019

	gen year = 2019
	sort idPers
	
	* Import main job
	merge m:1 idPers idFirm year using `main', keep(master matched) nogen
	bysort year idPers: gen multiple = _N
	drop if multiple > 1 & main != 1 // not main job
	keep idPers year occupation2019 sector2019
	drop if occupation2019 == ""
	drop if occupation2019 == "0000" // missing occupation
	
	save "$dta\ImportOccup19.dta",replace
